import { message } from 'antd';
import api from '../../api';

export default {
  namespace: 'stationModel',
  state: {
    data: [], //岗位列表数据
    loading: true,
    childrenGLGW: [], //岗位权限数据
    userData: [], //关联用户数据
    userOriginData: [], //未处理的关联用户数据
    tenantData: [], // 租户数据
  },

  effects: {
    // 租户数据
    *SearchTenant({ payload }, { call, put }) {
      const data = yield call(api.userManageNew.SearchTenant, payload);
      if (data && data.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tenantData: data.data.dataSource,
            loading: false,
          },
        });
      } else {
        message.error(`获取租户信息失败，失败原因：${data?.msg}`);
        console.error(`获取租户信息失败，失败原因：${data?.msg}`, '接口名称：page-query 接口：portfolio-server/tenant/page-query');
      }
    },
    // 岗位列表数据
    *Search({ payload }, { call, put }) {
      const data = yield call(api.stationManage.Search, payload);
      if (data && data.success) {
        let wjj = JSON.parse(JSON.stringify(data.data));
        let newData = [];
        wjj.map((element, i) => {
          element.newKey = '';
          element.postAuthority = '';
          element.stationAuthorities.forEach((item) => {
            if (item.name) {
              element.postAuthority = element.postAuthority + item.name + ' ';
              element.newKey = element.newKey + item.id + ' ';
            }
          });
          element.stationAuthorities = [];
          newData.push(element);
        });
        yield put({
          type: 'save',
          payload: {
            data: wjj,
            loading: false,
          },
        });
      } else {
        message.error(`岗位列表查询失败，失败原因：${data?.message}`);
        console.error(`岗位列表查询失败，失败原因：${data?.message}`, '接口名称：condition 接口：portfolio-server/station/condition');
      }
    },
    // 岗位权限下拉（暂无）
    *authority({ payload }, { call, put }) {
      const data = yield call(api.stationManage.authority, payload);
      if (data && data.success) {
        yield put({
          type: 'save',
          payload: {
            childrenGLGW: data.data,
          },
        });
      } else {
        message.error(`获取岗位权限数据失败，失败原因：${data?.message}`);
        console.error(`获取岗位权限数据失败，失败原因：${data?.message}`, '接口名称：authority 接口：portfolio-server/station/authority');
      }
    },
    // 新增编辑岗位
    *saveTable({ payload, callback }, { call, put }) {
      const data = yield call(api.stationManage.saveTable, payload);
      if (data && data.success) {
        callback('success');
        message.success(data?.mesg || '操作成功');
      } else {
        callback('fail');
        message.error(`保存失败，失败原因：${data?.mesg}`);
        console.error(`保存失败，失败原因：${data?.mesg}`, '接口名称：save 接口：portfolio-server/station/save');
      }
    },

    // 删除岗位
    *deleteById({ payload, callback }, { call, put }) {
      const res = yield call(api.stationManage.deleteById, payload);
      if (res && res.success == true) {
        callback('success');
        message.success(res?.mesg || '删除成功');
      } else {
        callback('fail');
        message.error(`删除岗位失败，失败原因：${res?.mesg}`);
        console.error(`删除岗位失败，失败原因：${res?.mesg}`, '接口名称：删除的id 接口：portfolio-server/station/delete/${query}');
      }
    },
    // 关联用户下拉数据
    *SearchUser({ payload }, { call, put }) {
      const data = yield call(api.stationManage.SearchUser, payload);
      if (data && data.success) {
        let tempArr = [];
        data.data &&
          data.data.map((item) => {
            tempArr.push({ title: item.name, value: item.id });
          });
        yield put({
          type: 'save',
          payload: {
            userData: tempArr,
            userOriginData: data.data,
          },
        });
      } else {
        message.error(`获取关联用户数据失败，失败原因：${data?.mesg}`);
        console.error(`获取关联用户数据失败，失败原因：${data?.mesg}`, '接口名称：findAll 接口：portfolio-server/user/findAll');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
    clearState(_, { payload }) {
      return payload;
    },
  },
};
